<?php
/**
* File that contains Application_Form_Admin_Login class.
* @package busmap
*/

/**
* Application_Form_Admin_Login class contains a form used for user login.
* There is a text field for the username, password field for the user password and a submit button for sending the data.
* @package busmap
*/
class Application_Form_Auth_Login extends Zend_Form
{
	/**
	* Initialize function generates and adds the necessary fields.
	*/
    public function init()
    {
        $this->setMethod('post');
 
        $this->addElement(
            'text', 'username', array(
                'label' => 'Kasutajanimi:',
                'required' => true,
                'filters'    => array('StringTrim'),
            ));
		$this->username->addErrorMessage('Kasutajanimi ei saa olla tühi');
 
        $this->addElement('password', 'password', array(
            'label' => 'Parool:',
            'required' => true,
            ));
 
		$this->password->addErrorMessage('Parool ei saa olla tühi');

        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Login',
            ));
 
    }

	/**
	* Overwritten isValid checks if there were any errors and if there were, it generates a error message decorator to the erroneous element.
	*/
	public function isValid($formData){
		$isValid = parent::isValid($formData);
		if(!$isValid) {
			$errors = $this->getErrors();
			foreach($errors as $fn => $error) {
			if(empty($error)) continue;
				$el = $this->getElement($fn);
				$dec = $el->getDecorator('HtmlTag');
				$dec->setOption('class', 'withErrors');
			}
		}
		return $isValid;
	}

}